import logging
import time
from warnings import simplefilter

import pandas as pd
import redis
from binance.um_futures import UMFutures
from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient

from . import BinanceKlineFactroy, BinanceKlinesTool

simplefilter(action='ignore')
UMF_WS = UMFuturesWebsocketClient()
logging.info("启动：UMF_WS")
UMF_WS.start()


class BinanceKlinesInit:
    symbol = ""
    now_price = 0
    now_date = ''

    klines_1m = pd.DataFrame({})
    klines_3m = pd.DataFrame({})
    klines_5m = pd.DataFrame({})
    klines_15m = pd.DataFrame({})
    klines_30m = pd.DataFrame({})
    klines_1h = pd.DataFrame({})

    divergence_1m = 0
    divergence_3m = 0
    divergence_5m = 0
    divergence_15m = 0
    divergence_30m = 0
    divergence_1h = 0

    def __init__(self, symbol):
        super().__init__()

        self.symbol = symbol
        self.__init_klines()

    def __init_klines(self):

        self.klines_1m = BinanceKlineFactroy.get_oder_data(self.klines_1m, self.symbol, "1m")
        self.klines_3m = BinanceKlineFactroy.get_oder_data(self.klines_3m, self.symbol, "3m")
        self.klines_5m = BinanceKlineFactroy.get_oder_data(self.klines_5m, self.symbol, "5m")
        self.klines_15m = BinanceKlineFactroy.get_oder_data(self.klines_15m, self.symbol, "15m")
        self.klines_30m = BinanceKlineFactroy.get_oder_data(self.klines_30m, self.symbol, "30m")
        self.klines_1h = BinanceKlineFactroy.get_oder_data(self.klines_1h, self.symbol, "1h")

        UMF_WS.kline(symbol=self.symbol, id=1, interval="1m", callback=self.__message_handler)
        UMF_WS.kline(symbol=self.symbol, id=2, interval="3m", callback=self.__message_handler)
        UMF_WS.kline(symbol=self.symbol, id=3, interval="5m", callback=self.__message_handler)
        UMF_WS.kline(symbol=self.symbol, id=4, interval="15m", callback=self.__message_handler)
        UMF_WS.kline(symbol=self.symbol, id=5, interval="30m", callback=self.__message_handler)
        UMF_WS.kline(symbol=self.symbol, id=6, interval="1h", callback=self.__message_handler)

    def __message_handler(self, message):
        if message.get('e'):
            date = message.get('E')
            open = message.get('k').get('o')
            close = message.get('k').get('c')
            high = message.get('k').get('h')
            low = message.get('k').get('l')
            volume = message.get('k').get('V')
            value = message.get('k').get('Q')

            interval = message.get('k').get('i')
            self.now_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(date / 1000))

            self.now_price = float(close)
            if interval == '1m':
                self.klines_1m = BinanceKlineFactroy.now_klines_concat(self.klines_1m,
                                                                       date=date,
                                                                       open=open,
                                                                       close=close,
                                                                       high=high,
                                                                       low=low,
                                                                       volume=volume,
                                                                       value=value,
                                                                       interval=interval)
                self.divergence_1m = BinanceKlinesTool.divergence(self.klines_1m)
            elif interval == '3m':
                self.klines_3m = BinanceKlineFactroy.now_klines_concat(self.klines_3m,
                                                                       date=date,
                                                                       open=open,
                                                                       close=close,
                                                                       high=high,
                                                                       low=low,
                                                                       volume=volume,
                                                                       value=value,
                                                                       interval=interval)
                self.divergence_3m = BinanceKlinesTool.divergence(self.klines_3m)
            elif interval == '5m':
                self.klines_5m = BinanceKlineFactroy.now_klines_concat(self.klines_5m,
                                                                       date=date,
                                                                       open=open,
                                                                       close=close,
                                                                       high=high,
                                                                       low=low,
                                                                       volume=volume,
                                                                       value=value,
                                                                       interval=interval)
                self.divergence_5m = BinanceKlinesTool.divergence(self.klines_5m)
            elif interval == '15m':
                self.klines_15m = BinanceKlineFactroy.now_klines_concat(self.klines_15m,
                                                                        date=date,
                                                                        open=open,
                                                                        close=close,
                                                                        high=high,
                                                                        low=low,
                                                                        volume=volume,
                                                                        value=value,
                                                                        interval=interval)
                self.divergence_15m = BinanceKlinesTool.divergence(self.klines_15m)
            elif interval == '30m':
                self.klines_30m = BinanceKlineFactroy.now_klines_concat(self.klines_30m,
                                                                        date=date,
                                                                        open=open,
                                                                        close=close,
                                                                        high=high,
                                                                        low=low,
                                                                        volume=volume,
                                                                        value=value,
                                                                        interval=interval)
                self.divergence_30m = BinanceKlinesTool.divergence(self.klines_30m)
            elif interval == '1h':
                self.klines_1h = BinanceKlineFactroy.now_klines_concat(self.klines_1h,
                                                                       date=date,
                                                                       open=open,
                                                                       close=close,
                                                                       high=high,
                                                                       low=low,
                                                                       volume=volume,
                                                                       value=value,
                                                                       interval=interval)
                self.divergence_1h = BinanceKlinesTool.divergence(self.klines_1h)

            self._kline_update()

    def _kline_update(self):
        pass
